const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		theme:app.theme,
		keyword: "",
		focusStatus: true,
		wordHistory: [],
		bol: true,
		isfocus: true
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			theme:app.theme,
			wordHistory: this.getKeyWordHistory()
		})
	},
	pushHistoryTolocal(keyword) {
		let word = [];
		this.data.wordHistory.forEach(element => {
			word.push(element);
		});
		let historyKeyWord = this.getKeyWordHistory();
		if (!!keyword.trim() && historyKeyWord.indexOf(keyword) < 0) {
			word.push(keyword);
			app.functions.setLocalStorage("keyword", word.join("|"));
		}
	},
	getKeyWordHistory() {
		const listWord = app.functions.getLocalStorage("keyword");
		return listWord ? listWord.split("|") : [];
	},
	keywordS(word) {
		this.setData({
			keyword: word.trim()
		})
    wx.navigateTo({
      url: "/pages_subpack/search_list/search_list?keyword=" + word.trim()
    });
		
	},
	handleChange(e) {
		this.setData({
			keyword: e.detail
		})
	},
	handlelook() {
		this.keywordS(this.data.keyword);
		this.pushHistoryTolocal(this.data.keyword);
	},
	enterSearch(e) {
		this.setData({
			keyword: e.detail,
			isfocus: false
		})
		this.keywordS(this.data.keyword);
		this.pushHistoryTolocal(this.data.keyword);
	},
	toSearchResult(e) {
		this.keywordS(e.currentTarget.dataset.key);
	},

	clearHistory() {
		wx.showModal({
			title: '温馨提示!',
			content: '是否清空历史记录',
			showCancel: true,
			cancelText: '取消',
			cancelColor: '#000000',
			confirmText: '确定',
			confirmColor: '#3CC51F',
			success: res => {
				if (res.confirm == true) {
					this.setData({
						wordHistory: []
					})
				}
			}
		});

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	}
})